<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="560px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="部门" prop="depts">
            <el-checkbox-group v-model="form.depts">
              <el-checkbox v-for="item in depts" :label="item.id" :key="item.id">{{item.name}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { addProcess, updProcess } from '@/api/process'
import { queryDepts } from '@/api/organ'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      depts: [],
      states: [{key: 0, value: '禁用'}, {key: 1, value: '启用'}],
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请填写名称' }],
        depts: [{ required: true, trigger: 'blur', message: '请选择部门' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        let dept_ids = this.info.edit.dept_ids
        if(dept_ids && dept_ids !== '') {
          this.$set(this.form, 'depts', dept_ids.split(','))
        }else{
          this.$set(this.form, 'depts', [])
        }
        this.queryDepts()
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    queryDepts() {
      queryDepts().then(response => {
        this.depts = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          this.$set(this.form, 'dept_ids', this.form.depts.join(','))
          if (this.info.type === 'add') {
            addProcess(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updProcess(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
